package main

// 用xorm实现读取表中的所有数据
import (
	"log"
	"time"

	_ "github.com/go-sql-driver/mysql"
	"xorm.io/xorm"
)

type AlertRule struct {
	Groups []Group `yaml:"groups"`
}

type Group struct {
	Name  string `yaml:"name"`
	Rules []Rule `yaml:"rules"`
}

type Rule struct {
	Alert       string            `yaml:"alert"`
	For         string            `yaml:"for,omitempty"`
	Annotations map[string]string `yaml:"annotations,omitempty"`
	Expr        string            `yaml:"expr"`
	Labels      map[string]string `yaml:"labels,omitempty"`
}

type StrategyRule2 struct {
	Id          string            `xorm:"notnull pk UUID 'id'"`
	RuleCode    string            `xorm:"varchar(64) unique notnull"`
	For         string            `xorm:"varchar(20)"`
	Annotations map[string]string `xorm:"varchar(256)"`
	RuleExpr    string            `xorm:"varchar(512) notnull"`
	Labels      map[string]string `xorm:"varchar(512)"`
	Summary     string            `xorm:"varchar(512)"`
	Deleted     int               `xorm:"int notnull"`
	CreateAt    time.Time         `xorm:"TIMESTAMP"`
	UpdateAt    time.Time         `xorm:"TIMESTAMP"`
}

type StrategyRule struct {
	Id          string            `xorm:"notnull pk UUID 'id'"`
	RuleCode    string            `xorm:"varchar(64) unique notnull"`
	For         string            `xorm:"varchar(512)"`
	Annotations map[string]string `xorm:"varchar(256)"`
	RuleExpr    string            `xorm:"varchar(512) notnull"`
	Labels      map[string]string `xorm:"varchar(256)"`
	Deleted     int               `xorm:"int notnull"`
	CreateAt    time.Time         `xorm:"TIMESTAMP"`
	UpdateAt    time.Time         `xorm:"TIMESTAMP"`
}

type PrometheusRule struct {
	Id          string            `xorm:"notnull pk UUID 'id'"`
	RuleCode    string            `xorm:"varchar(64) unique notnull"`
	For         string            `xorm:"varchar(512)"`
	Annotations map[string]string `xorm:"varchar(256)"`
	RuleExpr    string            `xorm:"varchar(512) notnull"`
	Labels      map[string]string `xorm:"varchar(256)"`
	Sharding    int               `xorm:"varchar(64)"`
	Deleted     int               `xorm:"int notnull"`
	CreateAt    time.Time         `xorm:"TIMESTAMP"`
	UpdateAt    time.Time         `xorm:"TIMESTAMP"`
}

func main() {
	engine, err := xorm.NewEngine("mysql", "root:devops2020@mysql@tcp(10.0.12.87:3306)/cluster_observer?charset=utf8")
	if err != nil {
		log.Fatal(err)
	}

	//var prometheusRules PrometheusRule
	//_, err1 := engine.Get(&prometheusRules)
	//if err1 != nil {
	//	log.Fatal(err)
	//}

	var rules []*StrategyRule
	err2 := engine.Find(&rules)
	if err2 != nil {
		log.Fatal(err)
	}

	//err = engine.Iterate(new(StrategyRule), func(idx int, bean interface{}) error {
	//	rule := bean.(*StrategyRule)
	//	rules = append(rules, rule)
	//	return nil
	//})

	//if err != nil {
	//	log.Fatal(err)
	//}

	//var alerts AlertRule
	//for _, rule := range rules {
	//	alerts.Groups = append(alerts.Groups, Group{
	//		Name: rule.RuleCode,
	//		Rules: []Rule{{
	//			Alert:       rule.RuleCode,
	//			For:         rule.For,
	//			Annotations: rule.Annotations,
	//			Expr:        rule.RuleExpr,
	//			Labels:      rule.Labels,
	//		}},
	//	})
	//}
	//
	//log.Printf("Rule Code: %s", rules[0].RuleCode)

}
